package android.medicalcloud.com.yiapp.yilian.ui;

import android.content.Intent;
import android.graphics.Color;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.logic.OrderController;
import android.medicalcloud.com.yiapp.yilian.model.OrderModel;
import android.medicalcloud.com.yiapp.yilian.model.PatitentModel;
import android.medicalcloud.com.yiapp.yilian.ui.MainPageActivity;
import android.medicalcloud.com.yiapp.yilian.ui.view.CommonTitleView;
import android.medicalcloud.com.yiapp.yilian.ui.view.dialog.SureDialog;
import android.medicalcloud.com.yiapp.yilian.utils.android.GsonTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.JsonTool;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.Serializable;
import java.util.List;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class OrderDetailActivity extends BaseActivity  implements ViewNetCallBack {
    CommonTitleView titleView;
    LinearLayout container;

    TextView stauesTv,codeTv,hospitalTv,departmentTv,typeTv,timeTv,titleRightTv,costTv;
    SureDialog sureDialog;
    boolean fromPush;
    @Override
    protected void initView() {
        titleView =(CommonTitleView) findViewById(R.id.titleView);
        titleView.setTitleText("订单详情");
        container = (LinearLayout)findViewById(R.id.container);
        stauesTv =(TextView) findViewById(R.id.stauesTv);
        hospitalTv =(TextView) findViewById(R.id.hospitalTv);
        departmentTv =(TextView) findViewById(R.id.departmentTv);
        typeTv =(TextView) findViewById(R.id.typeTv);
        timeTv =(TextView) findViewById(R.id.timeTv);
        codeTv =(TextView) findViewById(R.id.codeTv);
        titleRightTv =(TextView) findViewById(R.id.titleRightTv);
        costTv =(TextView) findViewById(R.id.costTv);

        titleRightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sureDialog.show();

            }
        });
        sureDialog  = new SureDialog(this);


    }

    @Override
    protected void initEvent() {

    }
    OrderModel model ;
    @Override
    protected void initData() {
         model = (OrderModel)getIntent().getSerializableExtra("m");

        if (model==null){
            fromPush = true;

            sureDialog.setOnSureClickLisener(new SureDialog.OnSureClickLisener() {
                @Override
                public void onSureClick() {
                    Logger.e("model == = ");
                    Logger.e("model == = "+model);
                    Logger.e("model == = ");
                    Logger.e("model == = "+model.getId());
                    Logger.e("model == = "+model.getStaus());
                    OrderController.getInstance().updateState(new ViewNetCallBack() {
                        @Override
                        public void onConnectStart(Object o) {
                        }
                        @Override
                        public void onConnectEnd() {
                        }
                        @Override
                        public void onFail(Exception e, Object o, String reslut) {
                        }
                        @Override
                        public void onData(Serializable result, int flag, boolean fromNet, Object o) {
                            if (JsonTool.getString(o.toString(),"status").equals("1")){
                                return;
                            }
                            IntentTool.startMain(OrderDetailActivity.this,1);
                            finish();
                            sendBroadcast(new Intent(MainPageActivity.action));
                        }
                    },model.getId(),model.getStaus());
                }
            });

            String id=getIntent().getStringExtra("id");
            if (StringTools.isNullOrEmpty(id)){
                finish();
                return;
            }
            OrderController.getInstance().orderDetail(this,id);
            return;
        }
        OrderController.getInstance().orderDetail(this,model.getId());
//        OrderController.getInstance().orderDetail(this,"399");

        sureDialog.setOnSureClickLisener(new SureDialog.OnSureClickLisener() {
            @Override
            public void onSureClick() {
                Logger.e("model == = ");
                Logger.e("model == = "+model);
                Logger.e("model == = ");
                Logger.e("model == = "+model.getId());
                Logger.e("model == = "+model.getStaus());
                OrderController.getInstance().updateState(new ViewNetCallBack() {
                    @Override
                    public void onConnectStart(Object o) {
                    }
                    @Override
                    public void onConnectEnd() {
                    }
                    @Override
                    public void onFail(Exception e, Object o, String reslut) {
                    }
                    @Override
                    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
                        if (JsonTool.getString(o.toString(),"status").equals("1")){
                            return;
                        }

                        finish();

                    }
                },model.getId(),model.getStaus());
            }
        });

        changeByStaus();
    }

    private void changeByStaus(){
        if (model==null){
            return;
        }
        Logger.e("-------model.getStaus()----"+model);
        stauesTv.setTextColor(Color.parseColor("#727d8b"));
        if (model.getStaus().equals("209")){
            stauesTv.setTextColor(Color.parseColor("#29ab91"));
        }
        if (model.getStaus().equals("206")||model.getStaus().equals("203")){
            stauesTv.setTextColor(Color.parseColor("#f15a4a"));
        }
        if (model.getStaus().equals("206")){
            sureDialog.setRes("是否执行订单",false);

        }
        if (model.getStaus().equals("203")){
            sureDialog.setRes("是否确认订单",false);

        }
        if (model.getStaus().equals("206")){
            titleRightTv.setVisibility(View.VISIBLE);
            titleRightTv.setText("执  行");
            return;
        }
        Logger.e("-------model.getStaus()----"+model.getStaus());
        if (model.getStaus().equals("203")){
            titleRightTv.setVisibility(View.VISIBLE);
            titleRightTv.setText("确  认");
            return;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_order_detail;
    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {

        Logger.e(o.toString()+"    orderDetail");
        try {
            model = GsonTool.jsonToEntity(JsonTool.getString(o.toString(),"orders"),OrderModel.class);
            Logger.e(model.getOrderStatusDescription()+"    getOrderStatusDescription");
            stauesTv.setText(model.getOrderStatusDescription());
            codeTv.setText("订单编号："+model.getCode());
            hospitalTv.setText("邀请医院："+model.getHospital());
            departmentTv.setText("申请科室："+model.getDepartment());
            typeTv.setText("会诊类型："+(model.getType().equals("100")?"手术":"门诊"));
            timeTv.setText("会诊时间："+model.getConsultationStartTime()+"～"+model.getConsultationEndTime());
            costTv.setText("会诊金额："+model.getServiceCost()+"元");
            container.removeAllViews();
            List<PatitentModel> ps =  model.getPatients();
            for (int i=0;i<ps.size();i++){
                View view = LayoutInflater.from(this).inflate(R.layout.view_pation_item,null,false);
                final PatitentModel m  = ps.get(i);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentTool.patient(OrderDetailActivity.this,m);
                    }
                });

               TextView nameTv = (TextView) view.findViewById(R.id.nameTv);
               TextView sexTv = (TextView) view.findViewById(R.id.sexTv);
               TextView ageTv = (TextView) view.findViewById(R.id.ageTv);
               TextView jibingTv = (TextView) view.findViewById(R.id.jibingTv);
               TextView decript = (TextView) view.findViewById(R.id.decript);
                ImageView fujianIv = (ImageView) view.findViewById(R.id.fujianIv);
                fujianIv.setVisibility(m.isHasAccessory()?View.VISIBLE:View.GONE);
                nameTv.setText("病人姓名："+m.getName());
                sexTv.setText("性别："+m.getSex());
                ageTv.setText("年龄："+m.getAge());
                jibingTv.setText("确诊疾病："+m.getDisease());
                decript.setText("疾病描述："+m.getSimpleDescribe());
  
                container.addView(view);
            }
            changeByStaus();

        }catch (Exception e){
            Logger.e(e.getLocalizedMessage(),e);
        }




    }
}
